package dateFitting;

import java.util.List;

public class Comparsion {
    public static double calculateFixedRatioError(DataRead.PartData data) {
        try {
            double baseTime = data.getTime1();
            double sumSquaredError = 0;

            // 固定比例
            double ratio3 = 0.8;
            double ratio5 = 0.76;
            double ratio10 = 0.7;

            int[] repetitions = {1, 3, 5, 10};
            double[] actualTotalTimes = {
                    data.getTime1(),
                    data.getTime3(),
                    data.getTime5(),
                    data.getTime10()
            };

            for (int i = 0; i < repetitions.length; i++) {
                int rep = repetitions[i];
                double actualTotal = actualTotalTimes[i];

                // 计算预测的总时间
                double predictedTotalTime;
                if (rep == 1) {
                    predictedTotalTime = baseTime;
                } else if (rep == 3) {
                    predictedTotalTime = baseTime * ratio3 * 3;
                } else if (rep == 5) {
                    predictedTotalTime = baseTime * ratio5 * 5;
                } else { // rep == 10
                    predictedTotalTime = baseTime * ratio10 * 10;
                }

                sumSquaredError += Math.pow(predictedTotalTime - actualTotal, 2);
            }

            return Math.sqrt(sumSquaredError / repetitions.length); // 均方根误差
        } catch (Exception e) {
            System.err.println("计算误差时出错: " + e.getMessage());
            return Double.MAX_VALUE;
        }
    }

    public static double evaluateDataSet(List<DataRead.PartData> dataSet) {
        if (dataSet == null || dataSet.isEmpty()) {
            return Double.MAX_VALUE;
        }

        double totalError = 0;
        for (DataRead.PartData data : dataSet) {
            totalError += calculateFixedRatioError(data);
        }
        return totalError / dataSet.size(); // 返回平均误差
    }

    public static void printPredictions(DataRead.PartData data) {
        try {
            double baseTime = data.getTime1();

            // 固定比例
            double ratio3 = 0.8;
            double ratio5 = 0.76;
            double ratio10 = 0.7;

            System.out.println("实际值 vs 固定比例模型预测值：");

            System.out.printf("1件: 实际 %.2f, 预测 %.2f\n",
                    data.getTime1(), baseTime);
            System.out.printf("3件: 实际 %.2f, 预测 %.2f\n",
                    data.getTime3(), baseTime * ratio3 * 3);
            System.out.printf("5件: 实际 %.2f, 预测 %.2f\n",
                    data.getTime5(), baseTime * ratio5 * 5);
            System.out.printf("10件: 实际 %.2f, 预测 %.2f\n",
                    data.getTime10(), baseTime * ratio10 * 10);

        } catch (Exception e) {
            System.err.println("打印预测时出错: " + e.getMessage());
        }
    }
}
